package com.zhao.modleclass.activity;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.RequiresApi;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.TypedValue;
import android.view.View;
import android.view.Window;
import android.widget.TextView;

import com.zhao.modleclass.R;
import com.zhao.modleclass.adapter.SC_TeacherAdapter;
import com.zhao.modleclass.fragment.SCTeacherListFragment;

import java.util.ArrayList;
import java.util.List;

public class SC_Teacher extends AppCompatActivity {
    private Toolbar toolbar;
    private TextView textView;
    //创建上下文对象
    private Context mContext = this;
    //申明化组件
    private ViewPager viewPager_SC_Teacher;
    private TabLayout tablayout_SC_Teacher;
    //申明集合
    private List<Fragment> mList = new ArrayList<>();
    //申明顶部导航条数组
    private String[] mArrTabTitle = null;
    private SC_TeacherAdapter adapter;
    private SCTeacherListFragment listFragment;

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sc__teacherl);
        //设置状态栏的颜色
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            Window window = getWindow();
            //获取样式中的属性值
            TypedValue typedValue = new TypedValue();
            this.getTheme().resolveAttribute(Color.parseColor("#FF4081"), typedValue, true);
            int[] attribute = new int[]{Color.parseColor("#FF4081")};
            TypedArray array = this.obtainStyledAttributes(typedValue.resourceId, attribute);
            int color = array.getColor(0, Color.parseColor("#FF4081"));
            array.recycle();
            window.setStatusBarColor(color);
        }
        intTooble();
        initData();
        iniView();
    }

    private void initData() {
        mArrTabTitle = getResources().getStringArray(R.array.SC_TeacherArr);
        //根据标题生成碎片
        for (int i = 0; i < mArrTabTitle.length; i++) {
            listFragment = new SCTeacherListFragment(i);
            //把碎片放入集合中
            mList.add(listFragment);
        }
    }

    private void iniView() {
        viewPager_SC_Teacher= (ViewPager) findViewById(R.id.viewpager_SC_Teacher);
        tablayout_SC_Teacher= (TabLayout) findViewById(R.id.tablayout_SC_Teacher);
        adapter=new SC_TeacherAdapter(mList,this.getSupportFragmentManager(), mArrTabTitle);
        viewPager_SC_Teacher.setOffscreenPageLimit(3);
        viewPager_SC_Teacher.setAdapter(adapter);
        //把tablayout加入到viewPager
       tablayout_SC_Teacher.setupWithViewPager(viewPager_SC_Teacher);
    }

    private void intTooble() {
        //主activity的toolbar初始化
        toolbar = (Toolbar) findViewById(R.id.toolbar_SC_Teacher);
        textView= (TextView) findViewById(R.id.textView_SC_Teacher);
        setSupportActionBar(toolbar);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }
}
